<?php

/**
 * Clientorder form base class.
 *
 * @method Clientorder getObject() Returns the current form's model object
 *
 * @package    symfony
 * @subpackage form
 * @author     Your name here
 */
abstract class BaseClientorderForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'idOrder'                  => new sfWidgetFormInputHidden(),
      'orderName'                => new sfWidgetFormInputText(),
      'textCategoryId'           => new sfWidgetFormPropelChoice(array('model' => 'Textcategories', 'add_empty' => false)),
      'targetGroupId'            => new sfWidgetFormPropelChoice(array('model' => 'Targetgroup', 'add_empty' => false)),
      'formalStyleId'            => new sfWidgetFormPropelChoice(array('model' => 'Formalstyle', 'add_empty' => false)),
      'personStyleId'            => new sfWidgetFormPropelChoice(array('model' => 'Personstyle', 'add_empty' => false)),
      'emotionStyleId'           => new sfWidgetFormPropelChoice(array('model' => 'Emotionstyle', 'add_empty' => false)),
      'formulateId'              => new sfWidgetFormPropelChoice(array('model' => 'Formulatetext', 'add_empty' => false)),
      'excludedWords'            => new sfWidgetFormInputText(),
      'urlImage'                 => new sfWidgetFormInputText(),
      'created_at'               => new sfWidgetFormDateTime(),
      'expires_at'               => new sfWidgetFormDateTime(),
      'updated_at'               => new sfWidgetFormDateTime(),
      'editionoptionsorder_list' => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'Editionoptions')),
      'optionalgroupsorder_list' => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'Optionaltargetgroups')),
      'ordermedia_list'          => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'Media')),
      'structureorder_list'      => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'Structure')),
    ));

    $this->setValidators(array(
      'idOrder'                  => new sfValidatorChoice(array('choices' => array($this->getObject()->getIdorder()), 'empty_value' => $this->getObject()->getIdorder(), 'required' => false)),
      'orderName'                => new sfValidatorString(array('max_length' => 150)),
      'textCategoryId'           => new sfValidatorPropelChoice(array('model' => 'Textcategories', 'column' => 'idTextCategories')),
      'targetGroupId'            => new sfValidatorPropelChoice(array('model' => 'Targetgroup', 'column' => 'idTargetGroup')),
      'formalStyleId'            => new sfValidatorPropelChoice(array('model' => 'Formalstyle', 'column' => 'idFormalStyle')),
      'personStyleId'            => new sfValidatorPropelChoice(array('model' => 'Personstyle', 'column' => 'idPersonStyle')),
      'emotionStyleId'           => new sfValidatorPropelChoice(array('model' => 'Emotionstyle', 'column' => 'idEmotionStyle')),
      'formulateId'              => new sfValidatorPropelChoice(array('model' => 'Formulatetext', 'column' => 'idFormulateText')),
      'excludedWords'            => new sfValidatorString(array('max_length' => 500, 'required' => false)),
      'urlImage'                 => new sfValidatorString(array('max_length' => 200, 'required' => false)),
      'created_at'               => new sfValidatorDateTime(array('required' => false)),
      'expires_at'               => new sfValidatorDateTime(array('required' => false)),
      'updated_at'               => new sfValidatorDateTime(array('required' => false)),
      'editionoptionsorder_list' => new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'Editionoptions', 'required' => false)),
      'optionalgroupsorder_list' => new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'Optionaltargetgroups', 'required' => false)),
      'ordermedia_list'          => new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'Media', 'required' => false)),
      'structureorder_list'      => new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'Structure', 'required' => false)),
    ));

    $this->validatorSchema->setPostValidator(
      new sfValidatorPropelUnique(array('model' => 'Clientorder', 'column' => array('idOrder')))
    );

    $this->widgetSchema->setNameFormat('clientorder[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Clientorder';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['editionoptionsorder_list']))
    {
      $values = array();
      foreach ($this->object->getEditionoptionsorders() as $obj)
      {
        $values[] = $obj->getIdeditionoption();
      }

      $this->setDefault('editionoptionsorder_list', $values);
    }

    if (isset($this->widgetSchema['optionalgroupsorder_list']))
    {
      $values = array();
      foreach ($this->object->getOptionalgroupsorders() as $obj)
      {
        $values[] = $obj->getIdoptionaltarget();
      }

      $this->setDefault('optionalgroupsorder_list', $values);
    }

    if (isset($this->widgetSchema['ordermedia_list']))
    {
      $values = array();
      foreach ($this->object->getOrdermedias() as $obj)
      {
        $values[] = $obj->getIdtextmedia();
      }

      $this->setDefault('ordermedia_list', $values);
    }

    if (isset($this->widgetSchema['structureorder_list']))
    {
      $values = array();
      foreach ($this->object->getStructureorders() as $obj)
      {
        $values[] = $obj->getIdstructure();
      }

      $this->setDefault('structureorder_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveEditionoptionsorderList($con);
    $this->saveOptionalgroupsorderList($con);
    $this->saveOrdermediaList($con);
    $this->saveStructureorderList($con);
  }

  public function saveEditionoptionsorderList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['editionoptionsorder_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(EditionoptionsorderPeer::IDORDER, $this->object->getPrimaryKey());
    EditionoptionsorderPeer::doDelete($c, $con);

    $values = $this->getValue('editionoptionsorder_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new Editionoptionsorder();
        $obj->setIdorder($this->object->getPrimaryKey());
        $obj->setIdeditionoption($value);
        $obj->save();
      }
    }
  }

  public function saveOptionalgroupsorderList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['optionalgroupsorder_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(OptionalgroupsorderPeer::IDCLIENTORDER, $this->object->getPrimaryKey());
    OptionalgroupsorderPeer::doDelete($c, $con);

    $values = $this->getValue('optionalgroupsorder_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new Optionalgroupsorder();
        $obj->setIdclientorder($this->object->getPrimaryKey());
        $obj->setIdoptionaltarget($value);
        $obj->save();
      }
    }
  }

  public function saveOrdermediaList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['ordermedia_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(OrdermediaPeer::IDCLIENTORDER, $this->object->getPrimaryKey());
    OrdermediaPeer::doDelete($c, $con);

    $values = $this->getValue('ordermedia_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new Ordermedia();
        $obj->setIdclientorder($this->object->getPrimaryKey());
        $obj->setIdtextmedia($value);
        $obj->save();
      }
    }
  }

  public function saveStructureorderList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['structureorder_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(StructureorderPeer::IDORDER, $this->object->getPrimaryKey());
    StructureorderPeer::doDelete($c, $con);

    $values = $this->getValue('structureorder_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new Structureorder();
        $obj->setIdorder($this->object->getPrimaryKey());
        $obj->setIdstructure($value);
        $obj->save();
      }
    }
  }

}
